package com.xnx3.wangmarket.plugin.innerDown.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;
import com.xnx3.wangmarket.plugin.innerDown.entity.InnerDownBranch;
import com.xnx3.wangmarket.plugin.innerDown.entity.UserBranch;
import com.xnx3.wangmarket.plugin.kefu_domain.entity.Kefu;
import com.xnx3.wangmarket.plugin.kefu_domain.bean.MQBean;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.func.AttachmentFile;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.j2ee.vo.UploadFileVO;
import com.xnx3.wangmarket.admin.entity.News;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.AliyunLog;
import com.xnx3.wangmarket.domain.mq.DomainMQ;

/**
 * 下载系统，用户管理、用户所属部门修改
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/innerDown/admin/user/")
public class AdminUserInnerDownPluginController extends BasePluginController{
	@Resource
	private SqlService sqlService;
	
	/**
	 * 用户列表
	 */
	@RequestMapping("/list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
	    Sql sql = new Sql(request);
	    /*
	     * 设置可搜索字段。这里填写的跟user表的字段名对应。只有这里配置了的字段，才会有效。这里没有配置，则不会进行筛选
	     * 具体规则可参考： http://note.youdao.com/noteshare?id=3ccef2de6a5cda01f95f832b02e356d0&sub=D53E681BBFF04822977C7CFBF8827863
	     */
	    sql.setSearchColumn(new String[]{"username", "nickname"});
	    sql.appendWhere("user.id > 1");
	    int count = sqlService.count("user", sql.getWhere());
	    Page page = new Page(count, 30, request);
	    sql.setSelectFromAndPage("SELECT user.id,user.username, user.nickname,user.authority,user.lasttime,plugin_innerdown_user_branch.branchid FROM user LEFT JOIN plugin_innerdown_user_branch ON plugin_innerdown_user_branch.userid = user.id ", page);
	    List<Map<String,Object>> list = sqlService.findMapBySql(sql);
	    
	    //将展示的列表数据记录传到页面以供显示
	    model.addAttribute("list", list);
	    //将分页信息传到页面以供显示底部分页
	    model.addAttribute("page", page);
	    return "plugin/innerDown/admin/user/list";
	}
	

	/**
	 * 编辑用户所属哪个部门，这里是弹出窗口修改
	 * @param userid 要编辑的是哪个用户，userid，必须传入
	 */
	@RequestMapping(value="setBranch${url.suffix}")
	public String setBranch(HttpServletRequest request, Model model,
			@RequestParam(value = "userid", required = false , defaultValue="0") int userid){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
		if(userid == 0){
			return error(model, "请传入用户id编号");
		}
		
		User user = sqlService.findById(User.class, userid);
		if(user == null){
			return error(model, "用户不存在");
		}
		
		UserBranch userBranch = sqlService.findById(UserBranch.class, userid);
		if(userBranch == null){
			//之前没指定过，那么模拟一个
			userBranch = new UserBranch();
			userBranch.setUserid(userid);
			userBranch.setBranchid(0);
		}
		
		model.addAttribute("userBranch", userBranch);
		model.addAttribute("user", user);
		return "plugin/innerDown/admin/user/set";
	}
	

	/**
	 * 设定某个用户，为其指定属于哪个部门
	 * @param userid 要指定的是哪个用户，用户的id , user.id
	 * @param branchid 指定这个用户属于什么部门，这个是部门的id， 对应 branch.id
	 */
	@RequestMapping(value="setBranchSave${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO setBranchSave(HttpServletRequest request,
			@RequestParam(value = "userid", required = false , defaultValue="0") int userid,
			@RequestParam(value = "branchid", required = false , defaultValue="0") int branchid){
		if(!haveSuperAdminAuth()){
			return error("无权使用");
		}
		
		if(userid == 0 || branchid == 0){
			return error("请传入用户编号或者部门编号");
		}
		
		UserBranch userBranch = sqlService.findById(UserBranch.class, userid);
		if(userBranch == null){
			//之前未存过，那么重新创建
			userBranch = new UserBranch();
			userBranch.setUserid(userid);
		}
		userBranch.setBranchid(branchid);
		sqlService.save(userBranch);
		
		return success();
	}
	
}
